package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("插入菜品")
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO)
    {
        log.info("新增菜品：{}" , dishDTO);
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO)
    {
        log.info("分页查询菜品：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据ids批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据ids删除菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids)
    {
        log.info("根据ids删除菜品：{}",ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }
    /**
     * 起售或停售菜品
     * @param id
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售或停售菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status ,@RequestParam Long id)
    {
        log.info("起售或停售菜品：{}" , status,id);
        dishService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id得到菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id得到菜品以及口味")
    public Result getById(@PathVariable Long id)
    {
        log.info("根据id得到菜品：{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("更新菜品")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO)
    {
        log.info("更新菜品：{}" , dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(@RequestParam Long categoryId)
    {
        log.info("根据分类id查询菜品：{}",categoryId);
        List<Dish> dishVOList = dishService.list(categoryId);
        return Result.success(dishVOList);
    }
}
